package com.example.spring.test;

import com.example.spring.bean.Husband;
import com.example.spring.bean.Wife;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class test {

    @Test
    public void testCD2() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring2.xml");
        Husband husband = applicationContext.getBean("h", Husband.class);
        System.out.println(husband);

        Wife wife = applicationContext.getBean("w", Wife.class);
        System.out.println(wife);
    }

    @Test // 单例+set的循环依赖,这个模式下没有任何问题
    /**原因：在这种模式下spring对Bean的管理主要分为清晰的两个阶段
     * 1.在spring容器加载的时候，实例化Bean，只要任意一个Bean实例化之后，马上进行曝光【不等属性赋值就曝光】
     * 2.曝光之后,在进行属性的赋值（调用set方法）
     *
     * 核心：实例化对象和对象的属性赋值分为两个阶段完成
     *
     * 注意：只有scope是singleton的情况下，Bean才会采取提前曝光的措施*/
    public void testCD() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        Husband husband = applicationContext.getBean("husbandBean", Husband.class);
        System.out.println(husband);

        Wife wife = applicationContext.getBean("wifeBean", Wife.class);
        System.out.println(wife);
    }
}
